﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace ArcEngine.Editor
{
	public partial class LogForm : DockContent
	{

		/// <summary>
		/// Constructor
		/// </summary>
		public LogForm()
		{
			InitializeComponent();

			Log.OnEvent += new EventHandler<LogEventArgs>(LogHandler);

		}



		/// <summary>
		///  Clear the log
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Clear_Click(object sender, EventArgs e)
		{
			LogBox.Text = "";
			Log.Clear();
			//UpdateErrorPannel();
		}



		/// <summary>
		/// Copy log to the clipboard
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CopyToClipboard_Click(object sender, EventArgs e)
		{
			if (LogBox.Text != "")
				Clipboard.SetText(LogBox.Text);
		}

		/*
				/// <summary>
				/// Updates the error / warning report count
				/// </summary>
				void UpdateErrorPannel()
				{
					string txt;

					if (Log.WarningCount > 0)
					{
						txt = Log.WarningCount + " Warning";
						if (Log.WarningCount > 1)
							txt += "s";
						WarningLabel.ToolTipText = txt;
						WarningLabel.Text = txt;
						WarningLabel.Visible = true;
					}
					else
					{
						WarningLabel.Visible = false;
					}


					if (Log.ErrorCount > 0)
					{
						txt = Log.ErrorCount + " Error";
						if (Log.ErrorCount > 1)
							txt += "s";
						ErrorLabel.ToolTipText = txt;
						ErrorLabel.Text = txt;
						ErrorLabel.Visible = true;
					}
					else
					{
						WarningLabel.Visible = false;
					}
				}

		*/


		/// <summary>
		/// Handles all log events
		/// </summary>
		/// <param name="level"></param>
		/// <param name="message"></param>
		public void LogHandler(object sender, LogEventArgs e)
		{
			if (e.Message == null)
				return;

			AddLog(e);

		}


		/// <summary>
		/// Adds a log the event form
		/// </summary>
		/// <param name="evt">Event to log</param>
		public void AddLog(LogEventArgs e)
		{
			Color oldcolor = LogBox.SelectionColor;
			switch (e.Level)
			{
				case LogLevel.Error:
				{
					LogBox.SelectionColor = Color.Red;
				}
				break;

				case LogLevel.Debug:
				{
					LogBox.SelectionColor = Color.Green;
				}
				break;

				case LogLevel.Fatal:
				{
					LogBox.SelectionColor = Color.Magenta;
				}
				break;

				case LogLevel.Warning:
				{
					LogBox.SelectionColor = Color.DarkGreen;
				}
				break;

				default:
				{
					LogBox.SelectionColor = Color.Black;
				}
				break;
			}

			LogBox.AppendText(e.Message + Environment.NewLine);
			if (e.Trace != null)
			{
				LogBox.AppendText(e.Trace + Environment.NewLine);
			}

			LogBox.SelectionColor = oldcolor;
			LogBox.Select(LogBox.Text.Length, 0);
			LogBox.ScrollToCaret();


		}

	}
}
